﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;

namespace Baosight.ZNH.ZMCT.UI.PopWindows
{
    // <summary>
    /// 选择人员页面
    /// </summary>
    /// Title:SelectField
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2012-06-07  高强   [创建]
    ///  
    public partial class SelectPerson : EFViewCmes
    {
        public SelectPerson()
        {
            InitializeComponent();
        }

        private void Commit_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            if (this.grid.EIBlock.GetRowCount() > 0)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCT22");
                eiInfo.Set("method", "insert");
                eiInfo.Set("fdModelId", this.fdModelId.Value);

                this.MakeCheckedBlock(eiInfo);

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, OnDoWorkCompleted);
            }
            else
            {
                base.OpenEFWindowAlert("请选择需要添加的接收人员！", true);
                this.TempButtonOpen(true);
                return;
            }
        }

        /// <summary>
        /// 新增后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// 将列表中的记录组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void MakeCheckedBlock(EIInfo eiInfo)
        {
            EIBlock block = new EIBlock("result");
            block.SetColumns(this.grid.EIBlock.GetColumns());
            block.SetRows(this.grid.GetCheckedEIRows());
            eiInfo.AddBlock(block);
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            EFWindow window = Parent as EFWindow;
            window.DialogResult = false;
            window.Close();
        }
    }
}
